package ym.controller.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Category;
import ym.service.CategoryService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台广告
 */
@RestController
@CrossOrigin
@RequestMapping("/back/category")
public class CategoryCon {
    @DubboReference
    private CategoryService categoryService;

    /**
     * 新增广告
     *
     * @param category
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/category')")
    public BaseResult add(Category category) {
        categoryService.add(category);
        return BaseResult.ok();
    }

    /**
     * 修改广告
     *
     * @param category
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(Category category) {
        categoryService.update(category);
        return BaseResult.ok();
    }

    /**
     * 删除广告
     *
     * @param categoryId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer categoryId) {
        categoryService.delete(categoryId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询广告
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer categoryId) {
        Category category = categoryService.findById(categoryId);
        return BaseResult.ok(category);
    }

    /**
     * 根据id查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/updateStatus")
    public BaseResult updateStatus(Integer categoryId) {
        categoryService.updateStatus(categoryId);
        return BaseResult.ok();
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Category> categorys = categoryService.findAll();
        return BaseResult.ok(categorys);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyAuthority('/category')")
    @GetMapping("/search")
    public BaseResult search(int page, int size) {
        Page<Category> brandPage = categoryService.search(page, size);
        return BaseResult.ok(brandPage);
    }

    /**
     * 根据类型分页查询
     *
     * @param categoryTypeId
     * @param page
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyAuthority('/category')")
    @GetMapping("/searchByType")
    public BaseResult searchByType(Integer categoryTypeId, int page, int size) {
        Page<Category> categoryPage = categoryService.searchByType(categoryTypeId, page, size);
        return BaseResult.ok(categoryPage);
    }

}

